home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / U-Z / VideoToolBox Folder / Demos / NoiseVBL.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-23  |  7.1 KB  |  200 lines  |  [TEXT/KAHL]

  1. /*
  2. NoiseVBL.c
  3.  
  4. There are two ways to synchronize your program to a video display. Apple
  5. recommends using the vertical blanking level interrupt (VBL). That approach is
  6. illustrated here. The other approach, which I normally prefer, is to use a side
  7. effect of loading the video cards clut: most video drivers don't return until
  8. the vertical blanking interval. The interrupt approach is more or less
  9. guaranteed to work on all video cards, since Apple more or less requires that it
  10. be supported by all video drivers. It's only disadvantage is that, obviously,
  11. interrupts must be enabled. I have the impression, though I haven't collected
  12. hard data, that interrupts steal a lot of time, depending on how busy AppleTalk,
  13. the disk, SCSI, keyboard, mouse, and even miscellaneous INITs that are driven by
  14. VBL interrupts. So when I really want to crank, to display movies, I prefer to
  15. use SetPriority(7) to temporarily suspend all interrupts. In that case the
  16. video-driver side effect approach is your only choice. However, be warned that
  17. some video drivers don't wait, and others take more than a frame to load the
  18. clut, so do some careful timing before you rely on it.
  19.  
  20. This program shows a noise movie, a dynamic random checkerboard, at 66.7 Hz! The
  21. key routine, which shows each frame, is in the file CopyBitsQuickly.c For best
  22. results you should allocate lots of memory to this program, e.g. 3MB. It
  23. pre-computes as many noise frames as space allows. After showing each frame once
  24. it cycles through them again. Thus to get a true impression of white noise you
  25. need to compute lots of noise frames, e.g. 100. This may require several
  26. megabytes, depending what your screen's pixel depth is set to.
  27.  
  28. HISTORY:
  29. 11/23/88 Denis Pelli wrote it.
  30.  
  31. 4/20/91 I updated this. It now has essentially no hardware dependencies. It does
  32. require that the video be in a slot, since it uses slot interrupts. A basic
  33. problem with the scheme is that interrupts seem to be shut out while the
  34. interrupt service routines are running. As a result the timing information is
  35. inaccurate since many 1 ms interrupts are lost during the call to
  36. CopyBitsQuickly(). I think the only solution to this would be to put the time
  37. consuming call to CopyBitsQuickly back in the main program, waiting for the
  38. interrupt service routine to give it permission to start each new frame.
  39.  
  40. 4/15/92 Removed all bugs and updated to work with THINK C 5. Now properly set A5
  41. in the interrupt service routines, using the method suggested in Tech Note 180.
  42. I removed the old attempt to disable AppleTalk, which now seems to hang up when
  43. it attempts to reenable AppleTalk. I also removed the clut manipulations.
  44.  
  45. 4/16/92    Put the noise in its own window. Run essentially continuously until
  46. user quits.
  47.  
  48. 8/19/92 Replaced obsolete TimeIt.c by new Timer.c. Timing seems to be fine now.
  49.  
  50. 8/22/92 dgp Moved most of the interrupt service routine code to 
  51. VBLInterruptServiceRoutine.c. Just for fun, I made the program use an alternate monitor,
  52. if available. Fixed a small bug that prevented display of noise on an alternate
  53. monitor if it had a different pixelSize than the main screen.
  54.  
  55. 9/15/92 dgp Updated to use new Timer.c.
  56.  
  57. 2/18/93    dgp    Added fpu test.
  58. 2/20/93    dgp    Call Require(), increased stack space.
  59. */
  60.  
  61. #include "VideoToolbox.h"
  62. #if THINK_C
  63.     #include <console.h>
  64. #endif
  65.  
  66. /* The user may wish to adjust these. WIDTH should be a multiple of 4. */
  67. #define MAXFRAMES    200        // number of frames in movie
  68. #define WIDTH    256            /* width of displayed noise movie, in pixels */
  69. #define HEIGHT    256            /* height of displayed noise movie, in pixels */
  70. #define RANDOMPHASE 1        /* randomly shift each movie frame */
  71.  
  72. /* This is just for reference. The original is in VideoToolbox.h
  73. struct VBLTaskAndA5 {
  74.     volatile VBLTask vbl;
  75.     long ourA5;
  76.     void (*subroutine)(struct VBLTaskAndA5 *vblData);
  77.     GDHandle device;
  78.     long slot;
  79.     volatile long newFrame;                // Boolean
  80.     volatile long framesLeft;            // count down to zero
  81.     long framesDesired;
  82.     void *ptr;                            // use this for whatever you want
  83. };
  84. typedef struct VBLTaskAndA5 VBLTaskAndA5;
  85. */
  86.  
  87. void main(void);
  88.  
  89. void main()
  90. {
  91.     int i,j,error;
  92.     int dx=32,dy=32,dt=1,duration;
  93.     long frames;
  94.     unsigned long seed;
  95.     double s;
  96.     static char string[64];
  97.     Rect r;
  98.     short noiseI=0,noiseN=MAXFRAMES;
  99.     static PixMap noiseImage[MAXFRAMES];
  100.     static short noiseSequence[MAXFRAMES]; /* shuffled sequence */
  101.     VBLTaskAndA5 noiseVBL;
  102.     WindowPtr window,oldPort;
  103.     EventRecord theEvent;
  104.     int frameDone;
  105.     Timer *timer;
  106.     
  107.     StackGrow(10000);
  108.     Require(gestalt8BitQD);
  109.     GetDateTime(&seed);
  110.     srand(seed);
  111.     /* INITIALIZE QuickDraw */
  112.     #if THINK_C
  113.         console_options.nrows = 3;
  114.         printf("\n");
  115.     #else
  116.         InitGraf((Ptr) &thePort);
  117.         InitFonts();
  118.         InitWindows();
  119.         InitCursor();
  120.     #endif
  121.  
  122.     // pick a screen
  123.     noiseVBL.device=GetScreenDevice(1);
  124.     if(noiseVBL.device==NULL)noiseVBL.device=GetScreenDevice(0);
  125.  
  126.     printf("Enter width of check in pixels (%d)?",dx);
  127.     gets(string);
  128.     sscanf(string,"%d",&dx);
  129.     printf("%d\n",dx);
  130.     printf("Enter height of check in pixels (%d)?",dy);
  131.     gets(string);
  132.     sscanf(string,"%d",&dy);
  133.     printf("%d\n",dy);
  134.     printf("Enter duration of check in frames (%d)?",dt);
  135.     gets(string);
  136.     sscanf(string,"%d",&dt);
  137.     printf("%d\n",dt);
  138.  
  139.     GetPort(&oldPort);
  140.     SetRect(&r,0,0,WIDTH,WIDTH);
  141.     CenterRectInRect(&r,&(*noiseVBL.device)->gdRect);
  142.     OffsetRect(&r,-(r.left%32),0);
  143.     window=NewCWindow(NULL,&r,"\pComputing noise ...",1,noGrowDocProc,(WindowPtr) -1L,0,0L);
  144.     SetPort(window);
  145.     for(i=0;i<MAXFRAMES;i++){
  146.         noiseImage[i].pixelSize=(**(**GetWindowDevice(window)).gdPMap).pixelSize;
  147.         noiseImage[i].bounds=window->portRect;
  148.         noiseImage[i].baseAddr=NULL;
  149.         error=MakeNoise1(dx,dy,RANDOMPHASE,&noiseImage[i]);
  150.         if(error)break;
  151.     }
  152.     noiseN=i;
  153.     SetPort(oldPort);
  154.     printf("Using %d different frames of noise.\n",noiseN);
  155.     for(i=0;i<noiseN;i++)noiseSequence[i]=i; /* initialize sequence */
  156.     FlushEvents(-1L,0);
  157.     printf("Hit any key to quit.\n");
  158.     SetWTitle(window,"\pNoiseVBL");
  159.     timer=NewTimer();
  160.     do{
  161.         SelectWindow(window);
  162.         Shuffle(noiseSequence,noiseN);    /* shuffle the images */
  163.         noiseI=0;
  164.         noiseVBL.subroutine=NULL;    // use default
  165.         error=VBLInstall(&noiseVBL,noiseVBL.device,MAXFRAMES);
  166.         if(error)PrintfExit("VBLInstall: error %d\n",error);
  167.         noiseVBL.vbl.vblCount=1;    // Enable interrupt service routine
  168.         while(!noiseVBL.newFrame);    // wait for first frame
  169.         noiseVBL.newFrame=0;
  170.         while(!noiseVBL.newFrame);    // wait for second frame
  171.         StartTimer(timer);
  172.         frames=noiseVBL.framesLeft;
  173.         while(noiseVBL.framesLeft>1){
  174.             if(noiseVBL.newFrame){
  175.                 noiseVBL.newFrame=0;
  176.                 CopyBitsQuickly((BitMap *)&(noiseImage[noiseSequence[noiseI]])
  177.                     ,(BitMap *)*((CGrafPtr)window)->portPixMap
  178.                     ,&noiseImage[0].bounds,&window->portRect,srcCopy,NULL);
  179.                 if(noiseVBL.framesLeft%dt==0){
  180.                     noiseI++;
  181.                     if(noiseI>=noiseN) {
  182.                         Shuffle(noiseSequence,noiseN);
  183.                         noiseI=0;
  184.                     }
  185.                 }
  186.             }
  187.         }
  188.         while(!noiseVBL.newFrame);    // wait for last frame
  189.         s=StopTimer(timer)/1000000.;
  190.         frames-=noiseVBL.framesLeft;
  191.         VBLRemove(&noiseVBL);
  192.         printf("%.1f Hz \r",frames/s);
  193.     }while(!GetNextEvent(keyDown+keyUp+mouseDown,&theEvent));
  194.     FlushEvents(-1L,0);
  195.     for (i=0;i<noiseN;i++) DisposPtr((Ptr) noiseImage[i].baseAddr);
  196.     DisposeWindow(window);
  197.     DisposeTimer(timer);
  198.     abort();
  199. }
  200.